<?php

/**
 * uniBoard Admin Application Controller
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-06 01:45:14 +0000 (Mon, 06 Sep 2010) $
 * $Revision: 59 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class app_admin{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Application
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create display handler
		$appCore -> output = new class_admin_output( $appCore);
		
		// Create session handler
		$appCore -> session = new class_session( $appCore);
		
		// Load language strings
		$appCore -> loadLanguageFile( 'commons');
		$appCore -> loadLanguageFile( 'acp_commons');
		
		// Are we logged in?
		if ( $appCore -> user['id'] > 0)
		{
			// Exceptions service
			try
			{
				// Map file exists?
				if ( !file_exists( ADMIN_ACTIONS_MAP_PATH))
				{
					throw new exc_critical( 'FILE', $appCore -> lang['acp_map_actions_not_found'], ADMIN_ACTIONS_MAP_PATH . $appCore -> lang['file_does_not_exists']);
				}
				
				// Include ACP actions map
				require_once( ADMIN_ACTIONS_MAP_PATH);
				
				// Map file correct?
				if ( !isset( $actions_map) || !is_array( $actions_map))
				{
					throw new exc_critical( 'FILE', $appCore -> lang['acp_map_actions_incorrect'], ADMIN_ACTIONS_MAP_PATH);
				}
			}
			catch ( exc_critical $exception)
			{
				// Stop script runtime
				exit( $exception);
			}
			
			// Detect action?
			if ( strlen( $appCore -> _REQ( 'act')) > 0)
			{
				// Reiterate actions
				foreach ( $actions_map as $section => $actions)
				{
					// In section?
					if ( in_array( $appCore -> _REQ( 'act'), $actions))
					{
						// Show ACP action in section
						$appCore -> output -> section = $section;
						$appCore -> output -> action = $appCore -> _REQ( 'act');
						
						// Break loop
						break;
					}
				}
				
				// Is action found?
				if ( empty( $appCore -> output -> action))
				{
					// Show ACP index
					$appCore -> output -> section = 'home';
					$appCore -> output -> action = 'index';
					
					// Load section language file
					$this -> appCore -> loadLanguageFile( 'acp_home');
					
					// Make session live longer
					$appCore -> session -> updateAdminSession();
					
					// Run action
					new action_index( $appCore);
				}
				else
				{
					// Start action
					$this -> runAction();
				}
			}
			else if ( strlen( $appCore -> _REQ( 'sect')) > 0)
			{
				// Reiterate actions
				foreach ( $actions_map as $section => $actions)
				{
					// In section?
					if ( $appCore -> _REQ( 'sect') == $section)
					{
						// Show ACP action in section
						$appCore -> output -> section = $section;
						$appCore -> output -> action = $actions[0];
						
						// Load section language file
						$this -> appCore -> loadLanguageFile( 'acp_' . $appCore -> output -> section);
						
						// Break loop
						break;
					}
				}
				
				// Is action found?
				if ( empty( $appCore -> output -> action))
				{
					// Show ACP index
					$appCore -> output -> section = 'home';
					$appCore -> output -> action = 'index';
				
					// Load section language file
					$this -> appCore -> loadLanguageFile( 'acp_home');
						
					// Make session live longer
					$appCore -> session -> updateAdminSession();
					
					// Run action
					new action_index( $appCore);
				}
				else
				{
					// Start action
					$this -> runAction();
				}
			}
			else
			{
				// Show ACP index
				$appCore -> output -> section = 'home';
				$appCore -> output -> action = 'index';
				
				// Load section language file
				$this -> appCore -> loadLanguageFile( 'acp_home');
				
				// Make session live longer
				$appCore -> session -> updateAdminSession();
								
				// Run action
				new action_index( $appCore);	
			}
		}
		else
		{	
			// Start index action
			new action_login( $appCore);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Runs admin action
	 *
	 */
	
	function runAction(){
		
		// Load section language file
		$this -> appCore -> loadLanguageFile( 'acp_' . $this -> appCore -> output -> section);
		
		// Load Help Files?
		if ( $this -> appCore -> output -> section == 'users')
		{
			// Load users lang file
			$this -> appCore -> loadLanguageFile( 'users');
		}
		
		// Create action name
		$action_name = 'action_' . $this -> appCore -> output -> action;
		
		// Make session live longer
		$this -> appCore -> session -> updateAdminSession();
		
		// Run action
		new $action_name( $this -> appCore);

	}
	
}